﻿using Avalonia.Controls.Primitives;
using Avalonia.Xaml.Interactivity;
using KinonekoSoftware.Studio.Models.Modules;

namespace KinonekoSoftware.Studio.Resources.Behaviors
{
    public sealed class DoubleClickEditBehavior : Behavior<TemplatedControl>
    {
        protected override void OnAttached()
        {
            if (AssociatedObject is null)
            {
                return;
            }
            AssociatedObject.PointerPressed += OnMouseDoubleClick;
            base.OnAttached();
        }

        private void OnMouseDoubleClick(object sender, PointerPressedEventArgs e)
        {
            if (e.ClickCount <= 1)
            {
                return;
            }

            if (AssociatedObject is TemplatedControl { DataContext: ModulePresenter p })
            {
                //
                //
                p.EditCommand?.Execute(null);
                e.Handled = true;
            }
        }

        protected override void OnDetaching()
        {
            AssociatedObject.PointerPressed -= OnMouseDoubleClick;
            base.OnDetaching();
        }
    }
}